//
//  SYMainViewController.swift
//  YANAWeibo
//
//  Created by mac on 16/5/11.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SVProgressHUD

class SYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let syTabBar = SYTabBar()
        
        syTabBar.SYdelegate = self
        
        syTabBar.composeButtonClousre = { [weak self] in
            
            if SYUserAccountViewModel.sharedUserAccount.isLogin {
                self?.showComposeVC()
            } else {
                SVProgressHUD.showErrorWithStatus("请先登录再来发射微博😈")
            }
        }
        
        //使用kvc设置可读属性
        setValue(syTabBar, forKey: "tabBar")
        
        addChildViewController(SYHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(SYMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(SYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(SYProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        //统一设置全局的tabbar
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        //设置tabbar的title和image
        //统一设置标题
        childController.title = title
        
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title
        
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        
        //修改字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)
        //添加子控制器
        let nav = UINavigationController(rootViewController: childController)
        addChildViewController(nav)
    }
    
    private func showComposeVC() {
    
        let composeVC = SYComposeViewController()
        
        let composeNav = UINavigationController(rootViewController: composeVC)
        
        presentViewController(composeNav, animated: true, completion: nil)
    }
}

extension SYMainViewController: SYTabBarDelegate {
    func didSelectedComposeButton() {
        print("控制器来自代理的哈哈哈😄")
    }
}






